/**
 * Created by zhu on 2016/1/30.
 */
$(function() {
    $.get(contextPath + "/fans/address/flag/true", function (data) {
        initAddress(data);
    });

        $('#order_push').on('click',function(){

            var addressId =  $('#addressId').val();
            var goodsId =  $('#goodsId').val();
            var note = $('#note').val();
            var length = $('.goodsId').length;
            var formMsg = {addressId:addressId,note:note};
            for(var i=0;i<length;i++){
                formMsg['goodsId'+i] = $($('.goodsId')[i]).val();
                formMsg['price'+i] = $($('.myPrice')[i]).text();
                formMsg['amount'+i] = $($('.myNumber')[i]).text();
            }
            formMsg['flag'] = length;

            $.post(contextPath+"/order/purchase",formMsg,function(data){
                if(data.status == 0){
                    window.location.href=contextPath+"/cart/paymentMode/"+data.text;
                }else{
                    alert("请添加收货地址！");
                    window.location.href= contextPath+"/my/add_address";
                }
            },'json')

        })

    });

//});
function initAddress(data){
    data = jQuery.parseJSON(data);
    $('#myadress').empty();

    $.each(data.list,function(i,item){
        if(i == 0){
            var temp = ' <div class="infoOfConsignee" onclick="address()">'+
                '    <div class="col01-15 colfloat">'+
                ' <span class="icon icon-search"></span>'+
                ' </div>'+
                ' <div class="col03-70 colfloat">'+
                ' <div class = "inner_info info_01">'+
                ' <div class = "inner_detail_left">'+item.fansName+'</div>'+
                ' <div class = "inner_detail_right">'+item.phone+'</div>'+
                '</div>'+
                '<div class = "inner_info info_02">'+item.fansAddress+'</div>'+
                '</div>'+
                '<div class="col02-15 colfloat">'+
                '<input type="hidden" id="addressId" name="addressId" value="'+item.id+'"/>'+
                '<span class="icon icon-right"></span>'+
                '</div>'+
                '</div>';
            $('#myadress').append(temp);
        }
    });



}